<html>
<head><meta charset="utf-8"><title>codegen projections · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/codegen.20projections.html">codegen projections</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210878785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/codegen%20projections/near/210878785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/codegen.20projections.html#210878785">(Sep 22 2020 at 14:35)</a>:</h4>
<p>Hi, it appears that <code>codegen_place</code> processes projections in reverse order. Is that really the case, and if so why? Also, is there a semantic difference between <code>Place</code> and <code>PlaceRef</code>?</p>
<p>For reference, I'm trying to codegen projections in our compiler and was wondering how Rust handles the difference between a single <code>Deref</code> which may copy the value (e.g. <code>let b = *a</code> where <code>a</code> is <code>&amp;Array</code>), and, for example, a chain projection <code>[Deref, Index]</code>, where we probably don't want to copy the value of the array before indexing it (e.g. <code>let c = a[1]</code> where <code>a</code> is <code>&amp;Array</code>).</p>



<a name="210888831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/codegen%20projections/near/210888831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/codegen.20projections.html#210888831">(Sep 22 2020 at 15:38)</a>:</h4>
<p>A place is a location in memory/a register, not a value. As an example in <code>let c = &amp;a[1]</code> let's assume that the type of <code>a</code> is <code>&amp;[u16]</code> and the value is <code>&lt;fat pointer pointing at 0x10 with len 3&gt;</code>. This would be represented as <code>mir::Rvalue::Ref(mir::Place { local: a, projection: [Deref, Index(1)] }))</code>  If <code>a</code> lives in register <code>r1</code> we could say that the place <code>a</code> is <code>Place::Reg(r1)</code>. When doing a <code>Deref</code> projection that will result in <code>Place::Addr { addr: 0x10, meta: Some(3) }</code>. Next <code>Index(1)</code> will result in <code>Place::Addr { addr: 0x10 + 1 * size_of&lt;u16&gt;(), meta: None }</code>. <code>mir::Rvalue::Ref</code> will then return a value of <code>0x10 + 1 * size_of&lt;u16&gt;()</code>.</p>



<a name="210983331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/codegen%20projections/near/210983331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Diekmann <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/codegen.20projections.html#210983331">(Sep 23 2020 at 10:19)</a>:</h4>
<p>I figured out now that <code>codegen_place</code> doesn't actually process projections in reverse order. It just appears as such since each projection is popped off the end but then only processed when coming back up from the recursion (that's probably why there's a fixme to make this iterative ;-) ). I've also realised that rvalues are processed in <code>codegen_rvalue</code> which gives me a good idea how to deal with the <code>Deref</code> problem described above.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>